<?php
/**
 * Description: 拉取中奖用户信息
 * Created by IntelliJ IDEA.
 * User: 玉坤
 * Date: 2021/7/19
 * Time: 18:42
 */

use Biz\model\NewProduct\NewProductLotteryLogModel;

ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
ini_set('memory_limit', '1024M');

require_once '/data/wwwroot/g_vendor/autoload.php';
try {

//用户质量
    $user_quality = [
        0 => 'normal',
        2 => 'XL',
        9 => 'B2',
        10 => 'B1',
        11 => 'B3',
    ];
//用户类型
    $user_type = [
        "user_common" => "普通用户",
        "auth_user_brand" => "品牌用户",
        "auth_user_designer" => "设计师用户",
        "auth_user_v" => "个人v认证",
        "auth_user_unauth_designer" => "未认证设计师",
        "auth_org_v" => "机构v认证",
        "auth_user_example" => "居住榜样用户 取消此类型",
    ];
// 认证标签
    $user_tag = [
        101 => "暂不认证",
        102 => "测试账号",
        103 => "疑似商业用户",
        104 => "待认证设计师",
        105 => "待认证品牌",
        106 => "待认证媒体",
        107 => "居住榜样备选",
    ];

    $where = [
        'get_data_status' => NewProductLotteryLogModel::GET_DATA_OFF,
    ];
    $lists = NewProductLotteryLogModel::select("*", $where);
    if (!$lists) {
        throw new Exception("未有相关符合条件数据");
    }
    $jd_ids = [];
    $uids = [];
    foreach ($lists as $list) {
        $series_id = $list['series_id'];
        $prize_id = $list['prize_id'];
        $res = \Doraemon\service\Lottery\Lottery::getAwardUserList($series_id, $prize_id);
        if (!$res) {
            throw new Exception("未拉取到中奖信息");
        }
        $log['params'] = [
            $series_id,
            $prize_id,
        ];
        $log['res'] = $res;
        \Doraemon\tools\Log::info('拉取抽奖数据', $log);

        $uids = array_column($res['award_user_list'], 'uid');

        if (!$uids) {
            var_dump('活动id' . $list['nid'] . '中奖用户不存在');
        }
        $info = \Biz\model\NewProduct\NewProductWikiModel::first(['id' => $list['nid']]);
        $brand_name = $info['brand_name'] ?? '';
        $type = $info['type'] ?? '';
        $user_info = \Doraemon\service\position\PositionApi::getMultiUserProfile($uids, ['uid', 'action_last_day_browse_goods_rank', 'action_count_fans', 'user_type']);
        foreach ($user_info as $item) {
            $nid = $list['nid'];
            $fans = $item['action_count_fans'] ?? 0;
            $type = $user_type[$item['user_type']] ?? '暂无';
            $quality = $user_quality[$item['quality']] ?? '暂无';
            $auth_tag = $user_tag[$item['auth_tag']] ?? '暂无';
            $action = $item['action_last_day_browse_goods_rank'] ?? '暂无';
            $str .= "<tr><td>" . $item['uid'] . "</td><td>" . $action . "</td><td>" . $fans . "</td><td>"
                . $type . "</td><td>" . $quality . "</td><td>" . $auth_tag . "</td><td>" . $nid . "</td><td>" . $brand_name . "</td><td>" . $type . "</td></tr>";
        }

    }
    $tuo = 'tuoruiqing@haohaozhu.com';
    $cyk = 'congyukun@haohaozhu.com';
    $gxd = 'gaoxudong@haohaozhu.com';
    $body = <<<HTML
<style>
table.gridtable {
    font-family: verdana,arial,sans-serif;
    font-size:11px;
    color:#333333;
    border-width: 1px;
    border-color: #666666;
    border-collapse: collapse;
}
table.gridtable th {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #666666;
    background-color: #dedede;
}
table.gridtable td {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #666666;
    background-color: #ffffff;
}
</style>
 
<!-- Table goes in the document BODY -->
<table class="gridtable">

<tr>
    <th>uid</th><th>活跃时间</th><th>粉丝</th><th>用户类型</th><th>用户质量</th><th>用户标识</th><th>活动id</th><th>品牌名称</th>
</tr>
    $str
</table>
HTML;

    \Biz\tools\Mail::send([$cyk, $tuo, $gxd], "抽奖用户信息", $body);
} catch (\Exception $e) {
    print $e->getMessage();
    exit();
}
var_dump('发送完成');